const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputVal:'',
    myDate:'',
    img1:'../../images/l.png',
    img2: '../../images/r.png',
    myList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log("info:", app.globalData.userInfo)
    var nickName = app.globalData.userInfo.nickName

    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp); 
    var Y = date.getFullYear();  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
    var currentDate = Y + '-' + M
    
    this.setData({
      myDate:currentDate,
      nickName: nickName
    })

    this.myRequest(currentDate)
  },

  checkMonth: function (i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  },

  myRequest: function (date){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data
        wx.request({
          url: app.globalData.URL + '/app/myinvites/proDetail?month=' + date + '&openid=' + openid + '&nickName=' + that.data.nickName,
          success: function (res) {
            console.log("/myinvites/proDetail", res)
            that.setData({
              
              myList: res.data.list
            })

          }
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  

  inputSearch:function(e){
    this.setData({
      inputVal:e.detail.value
    })
    console.log(this.data.inputVal)
  },

  enterSearch:function(e){
    console.log("点回车搜索",e)
    var name = e.detail.value
    this.setData({
      nickName: name
    })
    this.myRequest(this.data.myDate)
  },
  

  lastMonth:function(){
    this.setData({
      img1:'../../images/l1.png',
      img2:'../../images/r.png'
    })

    let currentDate = new Date(this.data.myDate);

    let lastDate = currentDate.setMonth(currentDate.getMonth() - 1);
    lastDate = new Date(lastDate)
    let lastYear = lastDate.getFullYear();
    let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

    lastDate = lastYear + '-' + lastMonth;
    console.log("lastDate", lastDate)

    this.setData({
      myDate:lastDate
    })
    this.myRequest(lastDate)
  },

  nextMonth: function () {
    this.setData({
      img1: '../../images/l.png',
      img2: '../../images/r1.png'
    })

    let currentDate = new Date(this.data.myDate);
    let nextDate = currentDate.setMonth(currentDate.getMonth() + 1);
    nextDate = new Date(nextDate);
    let nextYear = nextDate.getFullYear();
    let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

    nextDate = nextYear + '-' + nextMonth;
    console.log("nextDate", nextDate)

    this.setData({
      myDate: nextDate
    })
    this.myRequest(nextDate)
  }
})